package com.android.lovingpet.adopt.adoption;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.Model;
import com.android.lovingpet.mine.pet.PetModel;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ikata on 2018/3/20.
 */
public class AdoptionPresenter extends BasePresenter<IAdoptionContract.View> implements IAdoptionContract.Presenter{
    AdoptionModel adoptionModel;
    PetModel petModel;
    public AdoptionPresenter()
    {
        adoptionModel=new AdoptionModel();
        petModel=new PetModel();
    }

    @Override
    public void addAdopt(String name, String type, String birthday, int sterilize, int source, String description, int immune, String demand, String gender,@Nullable File video) {
        HashMap<String,String> params=new HashMap();
        params.put("name",name);
        params.put("type",type);
        params.put("birthday",birthday);
        params.put("sterilize", String.valueOf(sterilize));
        params.put("source", String.valueOf(source));
        params.put("description",description);
        params.put("immune", String.valueOf(immune));
        params.put("demand",demand);
        params.put("gender",gender);
        adoptionModel.addAdopt(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()==null) return;
                getView().showToast(msg);
                getView().hideProgress();
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                int id=bundle.getInt("id");
                //上传视频
                if (video!=null) {
                    uploadVideo(id, 2, 0, video);
                }
                else {
                    if (getView()==null) return;
                    getView().next();
                }
                getView().hideProgress();
            }
        });
    }

    public void uploadVideo(int id, int userType, int videoType, File video) {
        HashMap<String,String> params=new HashMap<>();
        params.put("id", String.valueOf(id));
        params.put("userType", String.valueOf(userType));
        params.put("videoType", String.valueOf(videoType));
        Model.uploadVideo1(params,video, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()==null)
                    return;
                getView().showToast(msg);
                getView().hideProgress();
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle.getInt("code")==0)
                    getView().next();
            }
        });
    }

    @Override
    public void sort(HashMap<String,String> params,int offset,int num) {
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        adoptionModel.sort(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()==null) return;
                getView().showToast(msg);
                getView().next();
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                assert bundle != null;
                ArrayList<Integer> list=bundle.getIntegerArrayList("ids");
                if (list!=null)
                {
                    for (Integer id : list) {
                        getPetInfo(id);
                    }
                }
                getView().next();
            }
        });
    }

    @Override
    public void sortBySex(String gender, int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("gender",gender);
        sort(params,offset,num);
    }

    @Override
    public void sortByAddress(String address, int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("address",address);
        sort(params,offset,num);
    }

    @Override
    public void sortByType(String type, int offset, int num) {
        HashMap<String,String> params=new HashMap<>();
        params.put("variety",type);
        sort(params,offset,num);
    }

    @Override
    public void getPetInfo(int id) {
        HashMap<String,String> params=new HashMap<>();
        params.put("id", String.valueOf(id));
        params.put("petType", String.valueOf(1));
        petModel.getPetInfo(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()==null)
                    return;
                getView().showToast(msg);
                getView().next();

            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                assert bundle != null;
                Pet pet=bundle.getParcelable("result");
                getView().addPet(pet);
                getView().next();
            }
        });
    }


}
